<?php

namespace App\Store\Services\Wxcoupon;

use App\Store\Models\Wxapp\WxappCouponCodeList;
use App\Store\Models\Wxapp\WxappSetting;
use Illuminate\Http\Request;
use EasyWeChat\Pay\Application;
class Wxcoupon
{
    public $app;
    public $config;
    public $setting;
    public function __construct($cp_id){
        $setting=WxappSetting::where([['cp_id',$cp_id]])->first();
        if(!$setting){
            return ['code'=>'001','msg'=>'未配置微信支付'];
        }
        $config = [
            'mch_id' => $setting->wxpay_mchid,
            // 商户证书
            'private_key' => storage_path().$setting->wxpay_key_path,
            'certificate' => storage_path().$setting->wxpay_cert_path,
            'platform_certs'=>[
                storage_path().$setting->wxpay_platform_certs_path
            ],
             // v3 API 秘钥
            'secret_key' => $setting->wxpay_key,
            'http' => [
                'throw'  => false, // 状态码非 200、300 时是否抛出异常，默认为开启
                'timeout' => 5.0,
            ],
        ];
        $this->setting=$setting;
        $this->config=$config;
        
        $this->app = new Application($config);
    }

    public function createcoupon($params){
        $api = $this->app->getClient();
        $params['belong_merchant']=$this->config['mch_id'];
        // print_r($params);exit;
        $response = $api->post('/v3/marketing/busifavor/stocks', [
          'body'=>json_encode($params)
        ]);
        // var_dump($response->getContent());exit;

        $res=json_decode($response->getContent(),true);
        if(isset($res['stock_id'])){
            return ['code'=>'000','msg'=>'创建成功','data'=>['stock_id'=>$res['stock_id']]];
        }else{
            return ['code'=>'001','msg'=>'创建失败'.(isset($res['message'])?('：'.$res['message']):'')];
        }
    }

    public function import($coupon_code_list,$upload_request_no,$stock_id){
        $api = $this->app->getClient();
        $params['belong_merchant']=$this->config['mch_id'];
        $response = $api->post('/v3/marketing/busifavor/stocks/'.$stock_id.'/couponcodes', [
          'body'=>json_encode([
            'coupon_code_list'=>$coupon_code_list,
            'upload_request_no'=>$upload_request_no,
          ])
        ]);
        $res=json_decode($response->getContent(),true);
        if(isset($res['success_codes'])){
            return ['code'=>'000','msg'=>'创建成功','data'=>['success_codes'=>$res['success_codes']]];
        }else{
            return ['code'=>'001','msg'=>'创建失败'.(isset($res['message'])?('：'.$res['message']):'')];
        }
    }





    public   function checkputcoupon($coupon_id,$user_order_id)
    {
        $info=WxappCouponCodeList::where(['id'=>$coupon_id])->first();
        $api = $this->app->getClient();
        //微信核券流程
        $response = $api->post('/v3/marketing/busifavor/coupons/use', [
            'body'=>json_encode([
              'coupon_code'=>$info->coupon_code,
              'stock_id'=>$info->stock_id,
              'appid'=>$this->setting->appid,
              'use_time'=>date('Y-m-d').'T'.date('H:i:s').'+08:00',
              'use_request_no'=>$coupon_id.date('YmdHis').time(),
            ])
        ]);

        $res_arr=json_decode($response,true);
        if(isset($res_arr['wechatpay_use_time'])){
            WxappCouponCodeList::where(['id'=>$coupon_id])->update(['is_used'=>2,'used_at'=>time(),'user_order_id'=>$user_order_id]);
        }

        

    }
}